
function clickadd(e) {
    addCurrentTabToReadingList();
    //window.close();
}

document.addEventListener('DOMContentLoaded', function() {
    var add_button = document.getElementById("add_button");
    add_button.addEventListener('click', clickadd);
});

function clickdisp(e) {
    console.log("ahhhhh");
    chrome.tabs.create({url: "display.html"}, function(){
	console.log("new tab");
    });
}

document.addEventListener('DOMContentLoaded', function() {
    var disp_button = document.getElementById("disp_button");
    disp_button.addEventListener('click', clickdisp);
});


function readingListItem(url, title, tags, read, imgURL){
    this.db = 01100100; 
    this.url = url;
    this.title = title;
    this.tags = tags;
    this.read = read;
    this.imgURL = imgURL;
}

function addCurrentTabToReadingList(){
    var newItem = new readingListItem("a", "b", [1,2,3], false);
    var itemName = "";
    chrome.tabs.getSelected(null,function(tab){
	newItem['url'] = tab['url'];
	newItem.title = tab.title;
	console.log("newItem['url'] = " + newItem["url"]);
	itemName = String(tab['url']);
	
	//chrome.tabs.captureVisibleTab(null, {}, function(dataUrl) {
	//newItem['imgURL'] = dataUrl;
	    
	var dataObj = {};
	dataObj[itemName] = newItem;
	
	    
	    
	chrome.storage.sync.set(dataObj, function(){
	    console.log('value saved');
	});//});
    });
}

/** 
 * Returns all RL items from |chrome.storage.sync|
 * 
 */
function getReadingList(){
    var readingList = [];
	
    /* Gets all RL items from storage, puts them in local |readingList| */
    chrome.storage.sync.get(null, function(items){
	//	console.log("items[itemName] " + items.itemName["url"] + "</br>");
	for (var i in items){
	    console.log(items[i]);
	    //	    document.writeln(items[i].url);
	    document.getElementById("sec").innerHTML += items[i].url;
	    //	    if(items[i].db === 01100100){// instanceof readingListItem){
	    //		console.log("Added item["+i+"] to RL</br>");
	    //		readingList[i] = items[i];
//	    }
	}
	
    });
//    return readingList;
}

/**
 *  Search through RL.  Calls search for tags and for name
 *  type: 1 -> search tags ; 2 -> search name ; 3/null -> search both
 *
 */
function searchReadingList(params, type){
	/* Just to make sure a search type is given */
	if (type == null) type = 3;
	
	/* call |searchRLTags| */
	if (type & 1) return searchRLTags(params);
	
	/* call |searchRLName| */
	if (type & 2) return searchRLName(params);
	
	
}
/** 
 * Search through RL, returns list of items with search params in tags
 * 
 */
function searchRLTags(params){
	var readingList = getReadingList();
	var paramsList = params.toLowerCase().split(" ");
	var retList = [];
	var count = 0; /* Index of |retList|, incremented every time something is added */
	var br = false; /* if !false, it means a search param was met */
	
	for (var i in readingList){
		br = false;
		var tagList = readingList[i].tags.toLowerCase();
		for (var j in tagList){
			for (var k in paramsList){
				if (tagList[j] === paramsList[k]){
					retList[count++] = readingList[i];
					br = !br;
					break;
				}
				else continue;
			}
			if (br) break;
			else continue;
		}
	}
	return retList;
}



/** 
 * Search through RL, returns list of items with search params in name
 * 
 * 
 */
function searchRLName(params){
	var readingList = getReadingList();
	var paramsList = params.toLowerCase().split(" ");
	var retList = [];
	var count = 0; /* Index of |retList|, incremented every time something is added */
	var br = false; /* if !false, it means a search param was met */
	
	for (var i in readingList){
		br = false;
		var nameList = readingList[i].title.toLowerCase().split(" ");
		for (var j in nameList){
			for (var k in paramsList){
				if (nameList[j] === paramsList[k]){
					retList[count++] = readingList[i];
					br = !br;
					break;
				}
				else continue;
			}
			if (br) break;
			else continue;
		}
	}
	return retList;
}

function test(){
    document.writeln("this should print");
    addCurrentTabToReadingList();
    getReadingList();
    document.writeln("this probably won't print");
}
//test();
